package com.fengshang.wedding.domain.model.internal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.fengshang.wedding.domain.Constants;
import com.fengshang.wedding.domain.model.api.ITopic;
import com.fengshang.wedding.domain.model.api.Topic;
import com.fengshang.wedding.domain.model.api.VisibilityLevel;

@Entity(name = Constants.TOPIC_ENTITY_NAME)
@NamedQuery(name = Constants.TOPIC_FIND_ALL, query = "SELECT t FROM Topic t ORDER BY t.postDate DESC")

public class TopicEntity implements ITopic,Serializable {
	 
	private static final long serialVersionUID = -7249615696360207187L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@Column(name = "title", nullable = false)
	private String title;

	@Temporal(value = TemporalType.TIMESTAMP)
	private Date postDate;
	
	@Column(name="content",nullable=false)
	private String content;
	
	@Enumerated(EnumType.ORDINAL)
	private VisibilityLevel visibilityLevel;
	
	@ManyToOne
	@JoinColumn(name = "USER_ID", nullable = false)
	@OrderBy("postDate DESC")
	private UserEntity owner;
	/**
	 * indicates TOPIC.USER_ID is the column to match against USER.ID to retrieve topics associated with a user. 
	 */
	@OneToMany(cascade = CascadeType.ALL,mappedBy = "topic")
	private Collection<MessageEntity> messages = new ArrayList<MessageEntity>(1);

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getPostDate() {
		return postDate;
	}

	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	
	public VisibilityLevel getVisibilityLevel() {
		return visibilityLevel;
	}

	public void setVisibilityLevel(VisibilityLevel visibilityLevel) {
		this.visibilityLevel = visibilityLevel;
	}
	
	
	public UserEntity getOwner() {
		return owner;
	}

	public void setOwner(UserEntity owner) {
		this.owner = owner;
	}
	
	
	public void addMessage(MessageEntity message){
		if(!messages.contains(message)){
			message.setTopic(this);
			messages.add(message);
		}
	}
	

	public Collection<MessageEntity> getMessages() {
		return messages;
	}

	public void setMessages(Collection<MessageEntity> messages) {
		this.messages = messages;
	}
	
	@Transient
	public TopicEntity importData(ITopic topic){
		if(topic!=null){
			this.title=topic.getTitle()==null?title:topic.getTitle();
			this.postDate=topic.getPostDate()==null?postDate:topic.getPostDate();
			this.content=topic.getContent()==null?content:topic.getContent();
			this.visibilityLevel=topic.getVisibilityLevel()==null?visibilityLevel:topic.getVisibilityLevel();
		}
		return this;
	}

	@Transient
	public ITopic getValueObject() {
		Topic topic=new Topic(this);
		topic.setOwner(owner.getValueObject());
		return topic;
	}
	
}
